import 'package:flutter/material.dart';
import 'package:getwidget/getwidget.dart';
import 'package:meal/constant/antd_icons.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/model/order_model.dart';
import 'package:meal/ui/scroll_refresh_config.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/styles.dart';
import 'package:meal/utils/tools.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class HistoryOrder extends StatefulWidget {
  final int status;

  const HistoryOrder({Key? key, required this.status}) : super(key: key);

  @override
  State<HistoryOrder> createState() => _HistoryOrderState();
}

class _HistoryOrderState extends State<HistoryOrder> {
  final RefreshController refreshController = RefreshController(initialRefresh: false);

  final List<OrderModel> pageList = [];

  @override
  void initState() {
    super.initState();
    pageList.add(OrderModel(
        username: '欧阳致远',
        time: '18:14',
        address: '北海市海城区广东南路同和水岸44栋6楼',
        days: 90,
        count: 15,
        overMinutes: 9,
        tecCost: 45.25,
        income: 78.36,
        orderNo: '12232964103521',
        createTime: '05-29 12:14',
        shops: [
          CommodityModel(name: '汉堡', iconUrl: '1.png'),
          CommodityModel(name: '可乐', iconUrl: '2.png'),
          CommodityModel(name: '鸡翅', iconUrl: '3.png'),
        ]));
  }

  @override
  Widget build(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: refreshController,
      onRefresh: onRefresh,
      onLoading: onLoading,
      child: listViewBuilder(),
    );
  }

  Widget listViewBuilder() {
    return ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        var item = pageList[index];
        return Container(
          margin: const EdgeInsets.only(top: Sizes.default10),
          padding:
              const EdgeInsets.symmetric(horizontal: Sizes.default15, vertical: Sizes.default15),
          decoration: const BoxDecoration(color: Colors.white),
          child: Column(
            children: [
              Container(
                alignment: Alignment.center,
                margin: const EdgeInsets.only(bottom: Sizes.default10),
                child: SizedBox(
                  width: 200,
                  height: Sizes.default20,
                  child: CustomPaint(
                    painter: _CustomPainter(),
                    child: Center(
                      child: Text('第7次交易成功',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.colorFf6, fontSize: Sizes.default12)),
                    )
                  ),
                ),
              ),
              Container(
                height: Sizes.default30,
                alignment: Alignment.centerLeft,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text.rich(
                      TextSpan(
                          text: '${item.username} ',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color363, fontSize: Sizes.default16),
                          children: [
                            WidgetSpan(
                                child: ToolsUtils.getUrlSvg(
                                    'https://cdn7.axureshop.com/demo/1525603/images/未完成订单__待处理__平台配送_/u5470.svg',
                                    width: 15,
                                    height: 14),
                                alignment: PlaceholderAlignment.middle)
                          ]),
                      textDirection: TextDirection.ltr,
                      textAlign: TextAlign.left,
                    ),
                    Wrap(
                      spacing: Sizes.default30,
                      children: [
                        ToolsUtils.getUrlSvg(
                            'https://cdn7.axureshop.com/demo/1525603/images/未完成订单__待处理__平台配送_/u5468.svg',
                            width: 13,
                            height: 16),
                        ToolsUtils.getUrlSvg(
                            'https://cdn7.axureshop.com/demo/1525603/images/未完成订单__待处理__平台配送_/u5467.svg',
                            width: 16,
                            height: 16)
                      ],
                    )
                  ],
                ),
              ),
              Container(
                alignment: Alignment.centerLeft,
                child: Text(item.address,
                    style: StylesUtils.customTextStyle(
                        fontSize: Sizes.default13, color: ColorsUtils.color363)),
              ),
              Container(
                height: Sizes.default30,
                alignment: Alignment.centerLeft,
                child: Text(
                  '近${item.days}天第${item.count}次下单',
                  style: StylesUtils.customTextStyle(
                      color: ColorsUtils.color797, fontSize: Sizes.default13),
                ),
              ),
              Container(
                margin: const EdgeInsets.only(top: Sizes.default15),
                child: Row(
                  children: [
                    Expanded(
                        child: Column(
                      children: [
                        Align(
                          alignment: Alignment.centerLeft,
                          child: Text('${item.time} 骑手已送达',
                              style: StylesUtils.customTextStyle(
                                  color: ColorsUtils.color363, fontSize: Sizes.default16)),
                        ),
                        Container(
                          alignment: Alignment.centerLeft,
                          margin: const EdgeInsets.only(top: Sizes.default10),
                          child: Wrap(
                            spacing: Sizes.default10,
                            children: [
                              Container(
                                constraints: const BoxConstraints.tightFor(width: 60, height: 18),
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                    color:
                                        ColorsUtils.parseColorFromHexString(ColorsUtils.colorFf6),
                                    borderRadius: BorderRadius.circular(2)),
                                child: Text(
                                  '平台配送',
                                  style: StylesUtils.customTextStyle(
                                      color: ColorsUtils.colorWhite, fontSize: Sizes.default12),
                                ),
                              ),
                              Text.rich(TextSpan(
                                  text: '骑手：张三丰 ',
                                  style: StylesUtils.customTextStyle(
                                      color: ColorsUtils.color797, fontSize: Sizes.default13),
                                  children: [
                                    WidgetSpan(
                                      child: Icon(
                                        AntdIcons.phoneFill,
                                        color: ColorsUtils.parseColorFromHexString('#999999'),
                                        size: Sizes.default14,
                                      ),
                                      alignment: PlaceholderAlignment.middle,
                                    )
                                  ]))
                            ],
                          ),
                        )
                      ],
                    )),
                    Center(
                      child: Text.rich(TextSpan(
                          text: '订单跟踪',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color006, fontSize: Sizes.default13),
                          children: [
                            WidgetSpan(
                                child: Icon(
                              AntdIcons.right,
                              size: Sizes.default14,
                              color: ColorsUtils.colorFromHexString(ColorsUtils.color797),
                            ))
                          ])),
                    )
                  ],
                ),
              ),
              Container(
                alignment: Alignment.centerLeft,
                margin: const EdgeInsets.symmetric(vertical: Sizes.default10),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Expanded(
                        child: Wrap(
                      spacing: Sizes.default5,
                      children: [
                        ...item.shops
                            .map((e) => GFImageOverlay(
                                height: 60, width: 60, image: ToolsUtils.assetImage(e.iconUrl)))
                            .toList(),
                        Container(
                          height: 60,
                          width: 60,
                          alignment: Alignment.center,
                          child: ToolsUtils.getUrlSvg(
                              'https://cdn7.axureshop.com/demo/1525603/images/未完成订单__待处理__平台配送_/u5489.svg',
                              width: 20,
                              height: 5),
                        )
                      ],
                    )),
                    Center(
                      child: Text.rich(TextSpan(
                          text: '共6件商品',
                          style: StylesUtils.customTextStyle(
                              color: ColorsUtils.color006, fontSize: Sizes.default13),
                          children: [
                            WidgetSpan(
                                child: Icon(
                              AntdIcons.right,
                              size: Sizes.default14,
                              color: ColorsUtils.colorFromHexString(ColorsUtils.color797),
                            ))
                          ])),
                    )
                  ],
                ),
              ),
              Container(
                margin: const EdgeInsets.only(top: Sizes.default10),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text('发票信息',
                        style: StylesUtils.customTextStyle(
                            fontSize: Sizes.default13, color: ColorsUtils.color363)),
                    Text('不需要',
                        style: StylesUtils.customTextStyle(
                            fontSize: Sizes.default13, color: ColorsUtils.color797))
                  ],
                ),
              ),
              Container(
                margin: const EdgeInsets.only(top: Sizes.default10),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text('技术服务费',
                        style: StylesUtils.customTextStyle(
                            fontSize: Sizes.default13, color: ColorsUtils.color363)),
                    Text('￥-${item.tecCost}',
                        style: StylesUtils.customTextStyle(
                            fontSize: Sizes.default13, color: ColorsUtils.color797))
                  ],
                ),
              ),
              Container(
                margin: const EdgeInsets.only(top: Sizes.default10),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text('本单预计收入',
                        style: StylesUtils.customTextStyle(
                            fontSize: Sizes.default13, color: ColorsUtils.color363)),
                    Text('￥${item.income}',
                        style: StylesUtils.customTextStyle(
                            fontSize: Sizes.default13, color: ColorsUtils.colorCc0))
                  ],
                ),
              ),
              Container(
                alignment: Alignment.centerLeft,
                margin: const EdgeInsets.only(top: Sizes.default25),
                child: Text('订单编号：${item.orderNo}',
                    style: StylesUtils.customTextStyle(
                        fontSize: Sizes.default12, color: ColorsUtils.color797)),
              ),
              Container(
                alignment: Alignment.centerLeft,
                margin: const EdgeInsets.only(top: Sizes.default5),
                child: Text.rich(TextSpan(
                    text: '交易快照：发生争议时，可作为判断依据',
                    style: StylesUtils.customTextStyle(
                        fontSize: Sizes.default12, color: ColorsUtils.color797),
                    children: [
                      WidgetSpan(
                          child: Icon(
                        AntdIcons.right,
                        size: Sizes.default13,
                        color: ColorsUtils.colorFromHexString(ColorsUtils.color797),
                      ))
                    ])),
              ),
              Container(
                alignment: Alignment.centerLeft,
                margin: const EdgeInsets.only(bottom: Sizes.default25, top: Sizes.default5),
                child: Text('下单时间：${item.createTime}',
                    style: StylesUtils.customTextStyle(
                        fontSize: Sizes.default12, color: ColorsUtils.color797)),
              ),
              Container(
                  alignment: Alignment.centerRight,
                  child: Wrap(
                    spacing: Sizes.default15,
                    children: [
                      SizedBox(
                        width: 100,
                        height: 30,
                        child: GFButton(
                          onPressed: () {},
                          text: '再次打印',
                          textStyle: StylesUtils.customTextStyle(
                              fontSize: Sizes.default13, color: ColorsUtils.color797),
                          type: GFButtonType.outline,
                          borderSide: BorderSide(color: ColorsUtils.rgba(204, 204, 204, 1)),
                          padding: EdgeInsets.zero,
                        ),
                      ),
                      SizedBox(
                        width: 100,
                        height: 30,
                        child: GFButton(
                          onPressed: () {},
                          text: '评价配送员',
                          textStyle: StylesUtils.customTextStyle(
                              fontSize: Sizes.default13, color: ColorsUtils.colorFf6),
                          type: GFButtonType.outline,
                          borderSide: BorderSide(color: ColorsUtils.rgba(255, 110, 83, 1)),
                          padding: EdgeInsets.zero,
                        ),
                      )
                    ],
                  )
              )
            ],
          ),
        );
      },
      itemCount: pageList.length,
      itemExtent: 510,
    );
  }

  Future<void> onRefresh() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));
    refreshController.refreshCompleted(resetFooterState: true);
    setState(() {});
  }

  Future<void> onLoading() async {
    /// 发起请求
    await Future.delayed(const Duration(milliseconds: 2000));

    /// if failed, use loadFailed(), if no data return, use LoadNodata()
    if (pageList.length > 50) {
      return refreshController.loadNoData();
    }

    /// 添加订单
    refreshController.loadComplete();
    setState(() {});
  }
}

class _CustomPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = ColorsUtils.parseColorFromHexString('#FEF0F0')
      ..style = PaintingStyle.fill
      ..strokeWidth = 1;
    Path path = Path()
      ..moveTo(0, 0)
      ..lineTo(size.height, size.height)
      ..lineTo(size.width - size.height, size.height)
      ..lineTo(size.width, 0)
      ..close();
    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return this != oldDelegate;
  }
}
